//
//  NSNumber.swift
//  vr_web
//
//  Created by 李旭峰 on 2020/7/13.
//

import Foundation
/// 支付价格类型，以分为单位
typealias PriceType = Int
extension PriceType {
    func toPrice() -> String {
        let meterText = (self.number * 0.01).numRoundStr(scale: 2, roundingMode: .down, suffixZero: false)
        return meterText
    }
    func toCNPrice() -> String {
        let meterText = (self.number * 0.01).numRoundStr(scale: 2, roundingMode: .down, suffixZero: false)
        return "¥" + meterText
    }
}
/// 面积类型，单位是平方厘米
//typealias AreaType = Int
extension Int {
    func toText() -> String {
        return "\(self)"
    }
    func toSquareMeterText() -> String {
        let meterText = (self.number * 0.0001).numRoundStr(scale: 2, roundingMode: .down, suffixZero: false)
        return String(format: "%@", meterText)
    }
    func toSquareMeterTextAdditionalUnit() -> String {
        let meterText = (self.number * 0.0001).numRoundStr(scale: 2, roundingMode: .down, suffixZero: false)
        return String(format: "%@㎡", meterText)
    }
}
typealias RateType = Double
extension RateType {
    
}

let kAssignScreenWidth:CGFloat = 375.0
let kAssignScreenHeight:CGFloat = 812.0
extension Int {
    var convert:CGFloat {
        return CGFloat(CGFloat(self)/kAssignScreenWidth) * kScreenWidth
    }
    var hConvert:CGFloat {
        return CGFloat(CGFloat(self)/kAssignScreenHeight) * kScreenHeight
    }
}
extension CGFloat{
    var convert:CGFloat {
        return CGFloat(CGFloat(self)/kAssignScreenWidth) * kScreenWidth
    }
}
extension Double {
    var convert:CGFloat {
        return CGFloat(CGFloat(self)/kAssignScreenWidth) * kScreenWidth
    }
    var hConvert:CGFloat {
        return CGFloat(CGFloat(self)/kAssignScreenHeight) * kScreenHeight
    }
}
extension Float {
    var convert:CGFloat {
        return CGFloat(CGFloat(self)/kAssignScreenWidth) * kScreenWidth
    }
    var hConvert:CGFloat {
        return CGFloat(CGFloat(self)/kAssignScreenHeight) * kScreenHeight
    }
}

public func + (left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber{
    let result = left.adding(right)
    return result
}

public func - (left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber{
    let result = left.subtracting(right)
    return result
}

public func * (left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber{
    let result = left.multiplying(by: right)
    return result
}

public func / (left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber{
    
    let roundingBehavior = NSDecimalNumberHandler.init(
        roundingMode: .down,
        scale: Int16(Int16.max),
        raiseOnExactness: false,
        raiseOnOverflow: false,
        raiseOnUnderflow: false,
        raiseOnDivideByZero: false)
    
    let result = left.dividing(by: right, withBehavior: roundingBehavior)
    return result
}
public func + (left: NSNumber, right: NSNumber) -> NSNumber{
    let result = (left.decimalValue + right.decimalValue)
    return result as NSNumber
}

public func - (left: NSNumber, right: NSNumber) -> NSNumber{
    let result = (left.decimalValue - right.decimalValue)
    return result as NSNumber
}

public func * (left: NSNumber, right: NSNumber) -> NSNumber{
    let result = (left.decimalValue * right.decimalValue)
    return result as NSNumber
}

public func / (left: NSNumber, right: NSNumber) -> NSNumber{
    let result = (left.decimalValue / right.decimalValue)
    return result as NSNumber
}

public func > (left: NSNumber, right: NSNumber) -> Bool{
    let result = left.compare(right)
    if result == .orderedDescending {
        return true
    }else {
        return false
    }
}

public func == (left: NSNumber, right: NSNumber) -> Bool{
    let result = left.compare(right)
    if result == .orderedSame {
        return true
    }else {
        return false
    }
}

public func < (left: NSNumber, right: NSNumber) -> Bool{
    let result = left.compare(right)
    if result == .orderedAscending {
        return true
    }else {
        return false
    }
}

public func <= (left: NSNumber, right: NSNumber) -> Bool{
    return !(left > right)
}

public func >= (left: NSNumber, right: NSNumber) -> Bool{
    return !(left < right)
}

extension NSNumber {
    
    func numRound(scale: Int , roundingMode: NSDecimalNumber.RoundingMode) -> NSDecimalNumber {
        return self.decimal.round(scale: scale, roundingMode: roundingMode)
    }
    /// 保留位数，返回字符串
    ///
    /// - Parameters:
    ///   - scale: 保留位数
    ///   - roundingMode: 模式 plain：四舍五入，up: 只入不舍， down：只舍不入， bankers：
    ///   - suffixZero: 位数不足时，是否补0，true：补0，false：不补0.  默认为false
    /// - Returns: 返回处理后的字符串
    func numRoundStr(scale: Int , roundingMode: NSDecimalNumber.RoundingMode, suffixZero: Bool = false) -> String {
        return self.decimal.roundStr(scale: scale, roundingMode: roundingMode, suffixZero: suffixZero)
    }
    
    func numRoundStr_up(_ scale: Int, suffixZero: Bool = false) -> String {
        return self.decimal.roundStr(scale: scale, roundingMode: .up, suffixZero: suffixZero)
    }
    
    
    func numRoundStr_down(_ scale: Int, suffixZero: Bool = false) -> String {
        return self.decimal.roundStr(scale: scale, roundingMode: .down, suffixZero: suffixZero)
    }
    
    func numRoundStr_plain(_ scale: Int, suffixZero: Bool = false) -> String {
        return self.decimal.roundStr(scale: scale, roundingMode: .plain, suffixZero: suffixZero)
    }
    
    func numRoundStr_bankers(_ scale: Int, suffixZero: Bool = false) -> String {
        return self.decimal.roundStr(scale: scale, roundingMode: .bankers, suffixZero: suffixZero)
    }
}
extension NSDecimalNumber {
    func round(scale: Int , roundingMode: NSDecimalNumber.RoundingMode) -> NSDecimalNumber {
        let roundingBehavior = NSDecimalNumberHandler.init(
            roundingMode: roundingMode,
            scale: Int16(scale),
            raiseOnExactness: false,
            raiseOnOverflow: false,
            raiseOnUnderflow: false,
            raiseOnDivideByZero: false)
        let result = self.rounding(accordingToBehavior: roundingBehavior)
        return result == NSDecimalNumber.notANumber ? NSDecimalNumber.init(value: 0): result
    }
    
    /// 保留位数，返回字符串
    ///
    /// - Parameters:
    ///   - scale: 保留位数
    ///   - roundingMode: 模式 plain：四舍五入，up: 只入不舍， down：只舍不入， bankers：
    ///   - suffixZero: 位数不足时，是否补0，true：补0，false：不补0.  默认为false
    /// - Returns: 返回处理后的字符串
    func roundStr(scale: Int , roundingMode: NSDecimalNumber.RoundingMode, suffixZero: Bool = false) -> String {
        
        let result = self.round(scale: scale, roundingMode: roundingMode)
        
        let digit = scale
        
        if suffixZero == false {
            return result.stringValue
        }else {
            var formatterString : String = "0."
            let count : Int = (digit < 0 ? 0 : digit)
            for _ in 0 ..< count {
                formatterString.append("0")
            }
            let formatter : NumberFormatter = NumberFormatter()
            formatter.positiveFormat = formatterString
            return formatter.string(from: result) != nil ? formatter.string(from: result)! : "0"
        }
    }
    
    func roundStr_up(_ scale: Int, suffixZero: Bool = false) -> String {
        return self.roundStr(scale: scale, roundingMode: .up, suffixZero: suffixZero)
    }
    
    
    func roundStr_down(_ scale: Int, suffixZero: Bool = false) -> String {
        return self.roundStr(scale: scale, roundingMode: .down, suffixZero: suffixZero)
    }
    
    func roundStr_plain(_ scale: Int, suffixZero: Bool = false) -> String {
        return self.roundStr(scale: scale, roundingMode: .plain, suffixZero: suffixZero)
    }
    
    func roundStr_bankers(_ scale: Int, suffixZero: Bool = false) -> String {
        return self.roundStr(scale: scale, roundingMode: .bankers, suffixZero: suffixZero)
    }
}

protocol ValueRoundingEnable {}

extension Int {
    var decimal: NSDecimalNumber {
        return  NSDecimalNumber.init(value: self)
    }
    var number: NSNumber {
        return NSNumber.init(value: self)
    }
    var cgfloat: CGFloat {
        return CGFloat(self)
    }
}

extension Double {
    var decimal: NSDecimalNumber {
        return NSDecimalNumber.init(value: self)
    }
    var number: NSNumber {
        return NSNumber.init(value: self)
    }
    var float:Float {
        return Float(self)
    }
    var cgfloat: CGFloat {
        return CGFloat(self)
    }
}

extension Float {
    var decimal: NSDecimalNumber {
        return NSDecimalNumber.init(value: self)
    }
    var number: NSNumber {
        return NSNumber.init(value: self)
    }
    var cgfloat: CGFloat {
        return CGFloat(self)
    }
}
extension CGFloat {
    var double:Double {
        return Double(self)
    }
    var float:Float {
        return Float(self)
    }
}

extension NSNumber {
    var decimal: NSDecimalNumber {
        return NSDecimalNumber.init(string: self.stringValue)
    }
}

extension NSNumber {
    var int: Int {
        return self.intValue
    }
    var float: Float {
        return self.floatValue
    }
    var double: Double {
        return self.doubleValue
    }
    var cgfloat: CGFloat {
        return CGFloat(self.floatValue)
    }
    //去除科学计数法
    var numString : String? {
        let numFormatter = NumberFormatter()
        numFormatter.maximumFractionDigits = 800
        numFormatter.minimumIntegerDigits = 1
        numFormatter.minimumFractionDigits = 0
        numFormatter.alwaysShowsDecimalSeparator = false
        let str = numFormatter.string(from: self)
        return str
    }
}

